package com.itheima.reggie.controller;

import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Page;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.entity.SetmealDish;
import com.itheima.reggie.entity.dto.DishDto;
import com.itheima.reggie.entity.dto.SetmealDto;
import com.itheima.reggie.entity.vo.SetmealDishVo;
import com.itheima.reggie.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/setmeal")
public class SetmealController {

    @Autowired
    private SetmealService setmealService;

    @PostMapping
    public R save(@RequestBody SetmealDto setmealDto, HttpSession session){
        // 1. 获取登录者，补全创建人与修改人
        Long empId = (Long) session.getAttribute("employee");
        setmealDto.setCreateUser(empId);
        setmealDto.setUpdateUser(empId);
        // 2.交给service
        setmealService.save(setmealDto);
        return R.success("保存成功！");
    }

    @GetMapping("/page")
    public R page(@RequestParam(defaultValue = "1") Integer page,
                  @RequestParam(defaultValue = "10") Integer pageSize,String name){
        Page<SetmealDto> pageResult = setmealService.findByPage(page,pageSize,name);
        return R.success(pageResult);
    }
    // 注意：如果形参是List集合，必须使用@RequestParam注解
    @DeleteMapping
    public R deleteByIds(@RequestParam List<Long> ids){
        setmealService.deleteByIds(ids);
        return R.success("成功删除！");
    }

    @GetMapping("/list")
    public R list(Long categoryId,Integer status){
        List<SetmealDto> setmealDtoList =  setmealService.list(categoryId,status);
        return R.success(setmealDtoList);
    }

    @GetMapping("/dish/{setmealId}")
    public R list(@PathVariable Long setmealId){
        System.out.println("setmealId = " + setmealId);
        List<SetmealDishVo> setmealDishList = setmealService.setmealDishList(setmealId);
        return R.success(setmealDishList);
    }
}
